<?php
	class mysql
	{
		private $connection;
		private $dbhost;
		private $dbuser;
		private $dbpass;
		private $dbname;
		private $last_result;
		private $debug_mode;
		private $debug_info;
		//public $fatal_error;
		public function __construct($host,$user,$pass,$dbname)
		{
			$this->dbhost = $host;
			$this->dbuser = $user;
			$this->dbpass = $pass;
			$this->dbname = $dbname;
			$this->debug_mode = defined('DEBUG_MODE') && DEBUG_MODE == TRUE;
			$this->debug_info = (object) array('queries' => array(),'time'=>0,);
			$this->connection = FALSE;
			$this->last_result = FALSE;
			//$this->fatal_error = FALSE;
		}
		public function connect()
		{
			$time = microtime(TRUE);
			$this->connection = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
			if(FALSE == $this->connection)
			{
				return FALSE;
			}
			$db = mysql_select_db($this->dbname,$this->connection);
			if(FALSE == $db)
			{
				return FALSE;
			}
			mysql_query('SET NAMES utf8',$this->connection);
			return $this->connection;
		}
		public function query($query,$remember_result=TRUE)
		{
			if(FALSE == $this->connection)
			{
				$this->connect();
			}
			$result = mysql_query($query,$this->connection);
			if(FALSE == $result)
			{
				return FALSE;
			}
			if($remember_result)
			{
				$this->last_result = $result;
			}
			return $result;
		}
		public function fetch_object($res=FALSE)
		{
			$res = FALSE !== $res? $res:$this->last_result;
			if(FALSE == $res)
			{
				return FALSE;
			}
			return mysql_fetch_object($res);
		}
		public function fetch($query)
		{
			$res = $this->query($query,FALSE);
			if(FALSE == $res)
			{
				return FALSE;
			}
			return $this->fetch_object($res);
		}
		public function fetch_all($query)
		{
			$res = $this->query($query,FALSE);
			if(FALSE == $res)
			{
				return FALSE;
			}
			$data = array();
			while($obj = $this->fetch_object($res))
			{
				$data[] = $obj;
			}
			$this->free_result($res);
			return $data;
		}
		public function fetch_field($query)
		{
			$res	= $this->query($query, FALSE);
			if(FALSE == $res)
			{
				return FALSE;
			}
			if( ! $row = mysql_fetch_row($res) )
			{
				return FALSE;
			}
			$this->free_result($res);
			return $row[0];
		}
		
		public function num_rows($res=FALSE)
		{
			$res    = FALSE!==$res ? $res : $this->last_result;
			if(FALSE == $res)
			{
				return FALSE;
			}
			return mysql_num_rows($res);
		}
		
		public function insert_id()
		{
			if(FALSE == $this->connection)
			{
				$this->connect();
			}
			return intval( mysql_insert_id($this->connection) );
		}
		
		public function affected_rows()
		{
			if(FALSE == $this->connection)
			{
				$this->connect();
			}
			return mysql_affected_rows($this->connection);
		}
		
		public function data_seek($row=0, $res=FALSE)
		{
			$res    = FALSE!==$res ? $res : $this->last_result;
			if(FALSE == $res)
			{
				return FALSE;
			}
			return mysql_data_seek($res, $row);
		}
		
		public function free_result($res=FALSE)
		{
			$res    = FALSE!==$res ? $res : $this->last_result;
			if(FALSE == $res)
			{
				return FALSE;
			}
			return mysql_free_result($res);
		}
		
		public function escape($string)
		{
			if(FALSE == $this->connection)
			{
				$this->connect();
			}
			return mysql_real_escape_string($string, $this->connection);
		}
		public function __destruct()
		{
			if(FALSE !== $this->connection)
			{
				@mysql_close($this->connection);
				$this->connection	= FALSE;
			}
		}
	}
?>